<?php

namespace app\controller\admin\sky;

use app\model\api\SkyCategory;
use laytp\controller\Backend;
use laytp\library\CommonFun;

/**
 * Class Author
 * @package app\controller\admin\peace
 */
class Goods extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $hasSoftDel=0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法


    public function _initialize()
    {
        $this->model = new \app\model\api\SkyGoods();
    }


    //添加
    public function add()
    {
        try {
            $post     = $this->request->post();
            $saveData = [
                'category1'    => $post['category1'],
                'target'       => $post['target'],
                'category'     => $post['category'],
                'img_url'      => $post['img_url'],
                'remark'      => $post['remark'],
                'name'         => $post['name'],
                'tag'          => $post['tag'],
                'sub_category' => $post['sub_category'],
                'category2'    => $post['category2'],
                'relation_img' => json_encode(explode(',',$post['relation_img']),JSON_UNESCAPED_UNICODE),
            ];
            $saveAll = $this->model->save($saveData);
            if (!$saveAll) throw new \Exception('保存失败');

            $this->clearCache();
            return $this->success('操作成功');
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }


    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $post = CommonFun::filterPostData($this->request->post());
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');

        $row->category1     = $post['category1'];
        $row->remark     = $post['remark'];
        $row->target        = $post['target'];
        $row->category      = $post['category'];
        $row->img_url       = $post['img_url'];
        $row->name          = $post['name'];
        $row->tag           = $post['tag'];
        $row->sub_category  = $post['sub_category'];
        $row->category2     = $post['category2'];
        $row->relation_img  = json_encode(explode(',',$post['relation_img']),JSON_UNESCAPED_UNICODE);
        try {
            $save = $row->save();
            if ($save === false) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * @return false|string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function filterIndex()
    {
        $where   = $this->buildSearchParams();
        $order   = $this->buildOrder();

        $data = $this->model->where($where)->order($order)
            ->select()->toArray();

        array_walk($data,function(&$item,$key){
            $item['search_name'] = $item['sub_category'].'-'.$item['category'].'-'.$item['category2'].'-'.$item['name'];
        });

        return $this->success('数据获取成功', $data);
    }


    /**
     * @return false|string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function filterGuide()
    {
        $data = (new SkyCategory())->field('sub_category as name')
            ->cache(true)
            ->select()->toArray();

        return $this->success('数据获取成功', $data);
    }


    /**
     * 获取详情
     * @return false|string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info(){
        $id = $this->request->param('id');

        $info = $this->model->where('id',$id)->find()->toArray();
        $info['tag'] = $info['tag'] ?? '';
        $info['relation_img'] = json_decode($info['relation_img'],true);

        return $this->success('获取成功', $info);
    }


}